﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
using Model;

namespace ComConverter
{
    public partial class MainForm : Form
    {
        private SerialPort mBmsSp;
        private SerialPort mWifiSp;
        public MainForm()
        {
            InitializeComponent();
            mBmsSp = new SerialPort();
            mWifiSp = new SerialPort();
            mBmsSp.DataReceived += mBmsSp_DataReceived;
            mWifiSp.DataReceived += mWifiSp_DataReceived;
        }


        void mWifiSp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                int bytes = mWifiSp.BytesToRead;
                byte[] buffer = new byte[bytes];
                bytes = mWifiSp.Read(buffer, 0, buffer.Length);
                if (mBmsSp.IsOpen)
                {
                    mBmsSp.Write(buffer, 0, bytes);
                }
            }
            catch (Exception)
            {
              
            }
        }

        void mBmsSp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                int bytes = mBmsSp.BytesToRead;
                byte[] buffer = new byte[bytes];
                bytes = mBmsSp.Read(buffer, 0, buffer.Length);
                if (mWifiSp.IsOpen)
                {
                    mWifiSp.Write(buffer, 0, bytes);
                }
            }
            catch (Exception)
            {
                
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            KillProcessForReadyStart();//杀死相关的进程
            bmsCbb.Items.Clear();
            wifiCbb.Items.Clear();
            var coms = SerialPort.GetPortNames();
            foreach (var cs in coms)
            {
                bmsCbb.Items.Add(cs);
                wifiCbb.Items.Add(cs);
            }
        }

        public static void KillProcessForReadyStart()
        {
            try
            {
                var PS = System.Diagnostics.Process.GetProcesses();
                //加载进程
                var content = GetContent();
                KillProcessModel model = Common.XmlSerializerTool.Deserialize(typeof(KillProcessModel), content) as KillProcessModel;
                foreach (var item in PS)
                {
                    foreach (var process in model.KillProcess)
                    {
                        if (item.ProcessName.Contains(process))
                        {
                            item.Kill();
                        }
                    }
                }
            }
            catch { }
        }

        private static string GetContent()
        {
            using (FileStream fs = new FileStream("KillConfiguration.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                List<byte> data = new List<byte>();
                while (true)
                {
                    int re = fs.ReadByte();
                    if (re != -1)
                    {
                        data.Add((byte)re);
                    }
                    else break;
                }
                string str = System.Text.Encoding.Default.GetString(data.ToArray());
                return str;
            }
        }

        bool tranFlag = false;
        private void convertBtn_Click(object sender, EventArgs e)
        {
            if (tranFlag==false)
            {
                if (bmsCbb.Text.Trim().Length < 2 || wifiCbb.Text.Trim().Length < 2)
                {
                    MessageBox.Show("请先选择相关的端口");
                    return;
                }

                try
                {
                    mWifiSp.PortName = wifiCbb.Text.Trim();
                    mWifiSp.BaudRate = 115200;
                    mBmsSp.PortName = bmsCbb.Text.Trim();
                    mBmsSp.BaudRate = 115200;
                    mWifiSp.Open();
                    mBmsSp.Open();
                    MessageBox.Show("数据转发启动成功");
                    convertBtn.Text = "关闭转发";
                    tranFlag = true;
                }
                catch (Exception)
                {
                    MessageBox.Show("打开端口失败，请检查端口是否被占用");
                    mWifiSp.Close();
                    mBmsSp.Close();
                } 
            }
            else
            {
                try
                {
                    mWifiSp.Close();
                    mBmsSp.Close();
                }
                catch { }
                MessageBox.Show("关闭转发成功");
                convertBtn.Text = "启动转发";
                tranFlag = false;
            }
         


        }
    }
}
